﻿using System;
using fastCSharp;

namespace diantou.examLogServer
{
    /// <summary>
    /// 考试日志
    /// </summary>
    [fastCSharp.code.cSharp.tcpCall(Service = "ExamLog")]
    [diantou.dataModel.sqlTable(Connection = diantou.dataModel.sqlTable.connection.ExamLog)]
    public partial class examLog : diantou.dataModel.examLog.sqlModel<examLog>
    {
        /// <summary>
        /// 日志类型
        /// </summary>
        public byte IntType
        {
            get { return (byte)Type; }
        }
        /// <summary>
        /// 获取用户考试日志
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [fastCSharp.code.cSharp.tcpMethod]
        private static examLog[] get(int userId)
        {
            list<examLog> logs = examUser.Get(userId).Log;
            return logs == null ? userCache[userId].ToArray() : logs.ToArray();
        }
        /// <summary>
        /// 添加考试日志
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="sessionId"></param>
        /// <param name="logs"></param>
        /// <returns></returns>
        [fastCSharp.code.cSharp.tcpMethod]
        private static bool append(int userId, int sessionId, examLog[] logs)
        {
            if (examUser.Get(userId).CheckSessionId(sessionId))
            {
                batchImport.Append(logs, true);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 批量导入日志
        /// </summary>
        private static readonly fastCSharp.sql.batchImport<examLog, dataModel.examLog> batchImport;
        /// <summary>
        /// 用户日志缓存
        /// </summary>
        private static readonly fastCSharp.sql.cache.part.memberQueueList<examLog, dataModel.examLog, examUser, int> userCache;
        static examLog()
        {
            if (sqlTable != null)
            {
                userCache = new fastCSharp.sql.cache.part.memberQueueList<examLog, dataModel.examLog, examUser, int>(sqlTable, value => value.UserId, examUser.Get, userId => value => value.UserId == userId, value => value.Log, value => value.PreviousLogNode, value => value.NextLogNode);

                batchImport = new fastCSharp.sql.batchImport<examLog, dataModel.examLog>(sqlTable, 1 << 10);
                sqlTable.LoadWait.Set();
                if (fastCSharp.config.appSetting.IsCheckMemory) checkMemory.Add(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
        }
    }
}
